package com.yunshang.shoppingmall.shopping.productApi.controller;

import com.yunshang.shoppingmall.common.ProductGuigePriceUtil;
import com.yunshang.shoppingmall.entity.*;
import com.yunshang.shoppingmall.service.PannerService;
import com.yunshang.shoppingmall.service.ProductADVService;
import com.yunshang.shoppingmall.service.ProductGuigePriceService;
import com.yunshang.shoppingmall.service.User.UserRankService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/wap")
public class ProductGuigePriceController {

    @Autowired
    ProductGuigePriceService productGuigePriceService;
    @Autowired
    PannerService pannerService;
    @Autowired
    ProductADVService productADVService;
    @Autowired
    UserRankService userRankService;

    //首页的零售专区数据
    @ResponseBody
    @RequestMapping("/selectProductGuigePriceBySignAndUpState")
    public ModelAndView selectProductGuigePriceBySignAndUpState(HttpServletRequest request){
    Map<String,Object> map=new HashMap<>();
        ModelAndView modelAndView=new ModelAndView("shoppingApi/product/productIndex");
        //panner图
        int pannerActiveSign=1;
        List<Panner> pannerList=pannerService.selectPanners(pannerActiveSign);
        System.out.println(pannerList.get(0).getPannerId());
        //首页热卖广告图
        String advSgin = "30";
        List<ProductADV> advList = productADVService.selectProductADVBySign(advSgin);
        map.put("advList1", advList.get(0));
        map.put("advList2", advList.get(1));
        map.put("advList3", advList.get(2));
        map.put("advList4", advList.get(3));
        map.put("pannerLists", pannerList);

        //用户是否登录
        int rankId=0;
        UserRank userRank=null;
        double commission=0.00;
        double minYHPrice=0.00;
        double fenxiangZhuan=0.00;
        UserNo userNo =(UserNo) request.getSession().getAttribute("userNo");
        if(userNo!=null){//用户登录
            rankId=userNo.getRank();
            userRank =userRankService.selectByPrimaryKey(rankId);
            commission=userRank.getCommission();//会员提成比
        }else{//没有登录，则按优质店主提成比
            userRank =userRankService.selectByPrimaryKey(18);
            commission=userRank.getCommission();//优质店主提成比
        }

        ProductGuigePrice productGuigePrice=new ProductGuigePrice();
        productGuigePrice.setActiveSign(4);
        productGuigePrice.setProductUpState("Y");

        List<ProductGuigePrice> productGuigePriceList =productGuigePriceService.selectProductGuigePriceByActiveSignAndUpState(productGuigePrice);
        for (int i = 0; i < productGuigePriceList.size(); i++) {
            productGuigePrice=productGuigePriceList.get(i);
            System.out.println("-----------commission----------->"+commission);
            minYHPrice=ProductGuigePriceUtil.getMinyhPrice(productGuigePrice,commission);//最低优惠价格
//            fenxiangZhuan=ProductGuigePriceUtil.getFenXiangZhuan(productGuigePrice,commission);//分享赚
            productGuigePrice.setMinYHPrice(minYHPrice);//最低优惠
            System.out.println("--------分享赚------->"+fenxiangZhuan);
            productGuigePrice.setFenxiangZhuan(fenxiangZhuan);//分享赚

        }


        map.put("productGuigePriceList",productGuigePriceList);

        System.out.println( productGuigePriceList.size());

        modelAndView.addObject("map",map);
        return modelAndView;
    }





}
